// 导入
require('../css/commCss/normalize.css')
require('../css/commCss/reset.css')
require('../css/commCss/property.css')
require('../css/commCss/basic.less')
require('../font/iconfont.css')
require('../css/motion-curriculum.less')
// 导入工具js
const util = require('../util/util')

// 导入axios
const axios = require('axios')

document.addEventListener('DOMContentLoaded', function () {
    const newCourse = document.querySelector('#newCourse')
    const oldClass = document.querySelector('#oldClass')

    // console.log(newCourse);

    // 顶部导航
    util.sportHeaderNav('course')

    // 底部导航
    util.fixBottomNav('motion')

    // 发送请求接收健身课程
    axios.get('http://139.9.177.51:8099/sports/allcourse').then(function (res) {
        // console.log(res.data.data);
        let data = res.data.data
        let oldHtml = ''
        oldHtml = data.map(function (v) {
            // console.log(v);
            // 判断是否是最新课程
            if (v.latest === 1) {
                document.querySelector('#newCourseImg').src = `http://139.9.177.51:8099${v.imgurl}`
                document.querySelector('#name').textContent = v.name
                document.querySelector('#desc').textContent = v.desc
                newCourse.dataset['cid'] = v.courseId
                // console.log(1);
            } else {
                return `
                    <div class="plain-course" data-cid="${v.courseId}">
                        <img src="http://139.9.177.51:8099${v.imgurl}" alt="">
                        <a class="title">${v.name}</a>
                        <a class="suggest">${v.desc}</a>
                    </div>
                `
            }
        })
        // console.log(oldHtml);
        oldClass.innerHTML = oldHtml.join('')
    })

    // 单渲染
    newCourse.addEventListener('click', function () {
        // 将cid保存到会话存储中
        sessionStorage.cid = this.dataset['cid'];
        // 跳转到详情页面
        location.href = './motion-details.html'

    })
    // 多渲染使用事件委派进行选中
    oldClass.addEventListener('click', function (e) {
        // 将cid保存到会话存储中
        sessionStorage.cid = e.target.closest('div').dataset['cid'];
        // 跳转到详情页面
        location.href = './motion-details.html'
    })

})